using BitBenderGames;
using DG.Tweening;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Mars.Got.Timer;

namespace Yoozoo.Gameplay.City
{
    public class CityBuildingPreShowBox : MonoBehaviour
    {
        [SerializeField]
        private Transform _Background;
        [SerializeField]
        private Transform _ConnerTopLeft;
        [SerializeField]
        private Transform _ConnerTopRight;
        [SerializeField]
        private Transform _ConnerBottomLeft;
        [SerializeField]
        private Transform _ConnerBottomRight;

        [SerializeField]
        private BoxCollider _BoxCollider;
        
        // 底不可建造颜色
        [SerializeField]
        private Color _BgCannotBuildColor;
        // 底可建造颜色
        [SerializeField]
        private Color _BgCanBuildColor;
        // 边框不可建造颜色
        [SerializeField]
        private Color _ConnerCannotBuildColor;
        // 边框可建造颜色
        [SerializeField]
        private Color _ConnerCanBuildColor;
        
        // 底需要染色的渲染器列表
        [SerializeField]
        private List<Renderer> _BgRenderers;
        // 边框需要染色的渲染器列表        
        [SerializeField]
        private List<Renderer> _ConnerRenderers;
        
        // 红点特效节点列表
        [SerializeField]
        private List<Transform> _ReddotEffects;
        // 红点特效隐藏节点列表
        [SerializeField]
        private List<Transform> _ReddotHideEffects;

        [Header("向内收缩")]
        [SerializeField] 
        private float _OffsetX = -1;
        [SerializeField]
        private float _OffsetZ = -1;

        private static MaterialPropertyBlock _MPB;

        /// <summary>
        /// 设置建筑的大小
        /// </summary>
        /// <param name="gridSizeX"></param>
        /// <param name="gridSizeZ"></param>
        public void SetBuildingSize(float gridSizeX, float gridSizeZ)
        {
            _Background.localScale = new Vector3(gridSizeX, gridSizeZ, 1);
            float halfX = gridSizeX / 2f + _OffsetX;
            float halfZ = gridSizeZ / 2f + _OffsetZ;
            _ConnerTopLeft.localPosition = new Vector3(-halfX, 0, -halfZ);
            _ConnerTopRight.localPosition = new Vector3(-halfX, 0, halfZ);
            _ConnerBottomLeft.localPosition = new Vector3(halfX, 0, -halfZ);
            _ConnerBottomRight.localPosition = new Vector3(halfX, 0, halfZ);
            _BoxCollider.size = new Vector3(gridSizeX, 10, gridSizeZ);
        }

        /// <summary>
        /// 设置是否可建造
        /// </summary>
        /// <param name="isVisible"></param>
        public void SetReddotEffectVisible(bool canBuild)
        {
            if(_MPB == null)
            {
                _MPB = new MaterialPropertyBlock();
            }
        
            // 设置底颜色
            Color color = canBuild ? _BgCanBuildColor : _BgCannotBuildColor;
            foreach(var renderer in _BgRenderers)
            {
                renderer.GetPropertyBlock(_MPB);
                _MPB.SetColor("_Color", color);
                renderer.SetPropertyBlock(_MPB);
            }
            // 设置边框颜色
            color = canBuild ? _ConnerCanBuildColor : _ConnerCannotBuildColor;
            foreach (var renderer in _ConnerRenderers)
            {
                renderer.GetPropertyBlock(_MPB);
                _MPB.SetColor("_Color", color);
                renderer.SetPropertyBlock(_MPB);
            }
            
            // 显示特效
            foreach(var effect in _ReddotEffects)
            {
                if (effect.gameObject.activeSelf != canBuild)
                {
                    effect.gameObject.SetActive(canBuild);
                }
            }
            // 隐藏特效
            foreach(var effect in _ReddotHideEffects)
            {
                if (effect.gameObject.activeSelf == canBuild)
                {
                    effect.gameObject.SetActive(!canBuild);
                }
            }
        }
        
    }
}
